package com.java.web;

import com.java.dao.commDao;
import com.java.dao.commDaoImpl;
import com.java.dao.userDao;
import com.java.dao.userDaoImpl;
import com.java.doMain.commodity;
import com.java.doMain.user;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author xyd
 * @Date 2021/12/23 17:36
 * @Version 1.0
 */
public class addCartServlet extends HttpServlet {

    private commDao commDao = new commDaoImpl();
    private userDao userDao = new userDaoImpl();

    /**
     *
     *  因为一个方法内只能运行一套流程
     *  在向用户表中添加商品ID的时候(购物车缓存问题)
     *  紧接着查询查询不到 所以需要跳转到另一个请求中查询/otherRequest
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        user user = (com.java.doMain.user) request.getSession().getAttribute("user");
        String cid = request.getParameter("cid");
        String cType = request.getParameter("cType");
        int id = Integer.parseInt(cid);
        if (user!=null){
            userDao.updateUserB2(user.getUserID(),user.getB2()+","+cid);
            request.getRequestDispatcher(request.getContextPath() + "/otherRequest").forward(request,response);
        }else {
            request.setAttribute("err","<h4><font color='red'>请先登录!</font></h4>");
            request.getRequestDispatcher(request.getContextPath() + "/client/login.jsp").forward(request,response);
        }

    }
}
